package com.geniusk.system.service.impl;

import com.geniusk.system.model.mapper.RoleGroupMapper;
import com.geniusk.system.model.pojo.Role;
import com.geniusk.system.model.pojo.RoleGroup;
import com.geniusk.system.service.RoleGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.geniusk.security.aspect.PreDataAuthAspect.DATA_AUTH_CUSTOM;

@Service
public class RoleGroupServiceImpl implements RoleGroupService {

    @Autowired
    private RoleGroupMapper roleGroupMapper;

    @Override
    public int insertRoleGroup(Role role) {
        roleGroupMapper.deleteRoleGroupByRoleId(role.getRoleId());
        Long[] groupIds = role.getGroupIds();
        if (groupIds != null && groupIds.length > 0 && role.getDataAuth().equals(DATA_AUTH_CUSTOM)) {
            // 新增角色组织关系
            List<RoleGroup> list = new ArrayList<>();
            for (Long groupId : groupIds) {
                RoleGroup roleGroup = new RoleGroup();
                roleGroup.setRoleId(role.getRoleId());
                roleGroup.setGroupId(groupId);
                list.add(roleGroup);
            }
            return roleGroupMapper.insertRoleGroup(list);
        }
        return 0;
    }

    @Override
    public List<String> selectGroupByRoleId(Long roleId) {
        return roleGroupMapper.selectGroupByRoleId(roleId);
    }
}
